<script setup lang="ts">
import { ref } from 'vue'

const list = ref([
    { id: 1, name: '《红楼梦》', author: '曹雪芹' },
    { id: 2, name: '《西游记》', author: '吴承恩' },
    { id: 3, name: '《水浒传》', author: '施耐庵' },
    { id: 4, name: '《三国演义》', author: '罗贯中' }
])

const del = (id: number) => {
    const index = list.value.findIndex(item => item.id)
    list.value.splice(index, 1)
    // list.value = list.value.filter(item => item.id !== id)
}
</script>


<template>
    <div id="app">
        <h3>小黑的书架</h3>
        <ul>
            <li v-for="item in list" :key="item.id">
                <span>{{ item.name }}</span>
                <span>{{ item.author }}</span>
                <!-- 注册点击事件 →  通过 id 进行删除数组中的 对应项 -->
                <button @click="del(item.id)" style="margin-left: 20px;">删除</button>
            </li>
        </ul>
    </div>
</template>

<style></style>